package com.xx.servlte;

import com.xx.dao.auction.AuctionDao;
import com.xx.entity.Auction;
import com.xx.service.AuctionService;
import com.xx.service.Impl.AuctionServiceImpl;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "AuctionServlet",urlPatterns = "/AuctionServlet")
public class AuctionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
        response.setCharacterEncoding("utf-8");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("action");
        switch (action) {
            case "auctionItem":
                //发布拍卖品
                auctionItem(request, response);
                break;
            case "select":
                select(request,response);
                break;
            case "selectGetAll":
                selectGetAll(request,response);
                break;
            case "deleteId":
                deleteId(request,response);
                break;
            case "bidding":
                bidding(request,response);
                break;
        }
    }



    private void bidding(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        //操作
        AuctionService dao=new AuctionServiceImpl();
        List<Auction> list1=dao.getAuction();
        request.setAttribute("list1",list1);
        request.getRequestDispatcher("Auctions.jsp").forward(request,response);
    }

    private void select(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        //操作
        AuctionService dao=new AuctionServiceImpl();
        List<Auction> list=dao.getAll();
        request.setAttribute("list",list);
        request.getRequestDispatcher("Auctionlist.jsp").forward(request,response);
    }

    private void deleteId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求数据
        Integer auctionId=Integer.valueOf(request.getParameter("auctionId"));
        //操作
        AuctionService dao=new AuctionServiceImpl();
        int ret=dao.delete(auctionId);
        PrintWriter out=response.getWriter();
        out.print(ret);
        out.flush();
        out.close();

    }

    private void selectGetAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        //操作
        AuctionService dao=new AuctionServiceImpl();
        List<Auction> list=dao.getAll();
        request.setAttribute("list",list);
        request.getRequestDispatcher("commodity.jsp").forward(request,response);
    }


//    private void updateAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//        //获取请求数据
//        String auctionName=request.getParameter("auctionName");
//        String auctionStartPrice=request.getParameter("auctionStartPrice");
//        String auctionUpset=request.getParameter("auctionUpset");
//        String auctionStartTime=request.getParameter("auctionStartTime");
//        String auctionEndTime=request.getParameter("auctionEndTime");
//        String auctionPic=request.getParameter("auctionPic");
//        String auctionPicType=request.getParameter("auctionPicType");
//        String auctionDesc=request.getParameter("auctionDesc");
//        Auction auction=new Auction(null,auctionName,auctionStartPrice,auctionUpset,auctionStartTime,auctionEndTime,auctionPic,auctionPicType,auctionDesc);
//        AuctionService dao=new AuctionServiceImpl();
//        dao.update(auction);
//        response.sendRedirect("commodity.jsp");
//    }

    private void auctionItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求数据
        String auctionName=request.getParameter("auctionName");
        String auctionStartPrice=request.getParameter("auctionStartPrice");
        String auctionUpset=request.getParameter("auctionUpset");
        String auctionStartTime=request.getParameter("auctionStartTime");
        String auctionEndTime=request.getParameter("auctionEndTime");
        String auctionPic=request.getParameter("auctionPic");
        String auctionPicType=request.getParameter("auctionPicType");
        String auctionDesc=request.getParameter("auctionDesc");
        //操作
        Auction auction=new Auction(null,auctionName,auctionStartPrice,auctionUpset,auctionStartTime,auctionEndTime,auctionPic,auctionPicType,auctionDesc);
        AuctionService dao=new AuctionServiceImpl();
        dao.save(auction);
        response.sendRedirect("commodity.jsp");
//        if (ret>0){
//            System.out.println("<script>alert(\"添加成功\");location.href='Auctionlist.jsp'</script>");
//            //response.sendRedirect("Auctionlist.jsp");
//        }else {
//            System.out.println("<script>alert(\"添加失败\");location.href='add.jsp'</script>");
//        }

    }
}
